package com.shopping.miaosha.controller;


import com.shopping.miaosha.controller.viewobject.UserObject;
import com.shopping.miaosha.entity.UserInfo;
import com.shopping.miaosha.entity.UserPassword;
import com.shopping.miaosha.error.ErrorCode;
import com.shopping.miaosha.exceptionHander.BusinessException;
import com.shopping.miaosha.response.ResultType;
import com.shopping.miaosha.service.IUserInfoService;
import com.shopping.miaosha.service.IUserPasswordService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author canger
 * @since 2019-03-25
 */
@RestController
@RequestMapping("/miaosha")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserPasswordService userPasswordService;

    @RequestMapping("/getUser")
    public ResultType getUser(@Param("id")String id) throws BusinessException {
        UserInfo userInfo = userInfoService.selectById(id);

        UserPassword userPassword = userPasswordService.selectById(id);

        if (userInfo == null){
            throw new BusinessException(ErrorCode.USER_NOT_EXIST);
        }
        System.out.println(userInfo);
        UserObject model = getModel(userInfo);
        return ResultType.getResult(model);
    }

    @ResponseStatus
    public UserObject getModel(UserInfo userInfo){
        UserObject userObject = new UserObject();
        BeanUtils.copyProperties(userInfo,userObject);
        return  userObject;
    }
}

